

module.exports = function () {
    this.__floaty_xml__ = (
        <relative id="main" w="auto" h="auto">
            <horizontal id="box"
                layout_toRightOf="st"
                visibility="gone"
                gravity="center_vertical"
                padding="10dp"
                layout_centerVertical="true"
            >
                {/* 启动暂停 */}
                <vertical marginRight="6dp" id="run_stop" visibility={this.mutations.to_icon_visibility(this.store.__need_switch__.runCheked)}>
                    <img w="30dp"
                        h="30dp"
                        scaleType="centerCrop"
                        id="run_stop_icon"
                        src={this.mutations.getImageBase("bofang")}
                        circle="true"
                        radius="20"
                        tint="#ffffff"
                        
                    ></img>
                    <text gravity="center" id="run_stop_text" text="启动" textColor="#ffffff" textSize="10sp"></text>
                </vertical>

                {/* 日志 */}
                <vertical marginRight="6dp" id="log" visibility={this.mutations.to_icon_visibility(this.store.__need_switch__.logChecked)}>
                    <img w="30dp"
                        h="30dp"
                        scaleType="centerCrop"
                        id="log_icon"
                        src={this.mutations.getImageBase("xianshi")}
                        
                        circle="true"
                        radius="20"
                        tint="#ffffff"
                        
                    ></img>
                    <text gravity="center" text="日志" textColor="#ffffff" textSize="10sp"></text>

                </vertical>

                {/* 表单 */}
                <vertical marginRight="6dp" id="from" visibility={this.mutations.to_icon_visibility(this.store.__need_switch__.fromChecked)}>
                    <img w="30dp"
                        h="30dp"
                        scaleType="centerCrop"
                        id="from_icon"
                        src={this.mutations.getImageBase("shezhi")}

                        circle="true"
                        radius="20"
                        tint="#ffffff"
                        
                    ></img>
                    <text gravity="center" text="设置" textColor="#ffffff" textSize="10sp"></text>
                </vertical>


                {/* 我的 */}
                <vertical marginRight="6dp" id="user" visibility={this.mutations.to_icon_visibility(this.store.__need_switch__.userChecked)}>
                    <img w="30dp"
                        h="30dp"
                        scaleType="centerCrop"
                        id="user_icon"
                        src={this.mutations.getImageBase("gerenzhongxin")}

                        circle="true"
                        radius="20"
                        tint="#ffffff"
                    ></img>
                    <text gravity="center" text="我的" textColor="#ffffff" textSize="10sp"></text>

                </vertical>


                {/* 退出 */}
                <vertical marginRight="6dp" id="quit" visibility={this.mutations.to_icon_visibility(this.store.__need_switch__.quitChecked)}>
                    <img w="30dp"
                        h="30dp"
                        scaleType="centerCrop"
                        id="quit_icon"
                        src={this.mutations.getImageBase("switch")}

                        circle="true"
                        radius="20"
                        tint="#ffffff"
                        
                    ></img>
                    <text gravity="center" text="退出" textColor="#ffffff" textSize="10sp"></text>
                </vertical>
            </horizontal>

            <img w="40dp"
                h="40dp" 
                scaleType="centerCrop" 
                id="st" 
                src={this.mutations.getImageBase("robot-3-7")}
                layout_centerVertical="true"
                marginRight="20dp"
                
            ></img>
        </relative>
    )

    this.__floaty_fun__ = function () {
        console.log("Hello World")

        let store = this.store
        let window = this.window
        let dh = this.dh
        let mutations = this.mutations
        let t = this

        let x = 0, y = 0, windowX, windowY, downTime;
    
        //载入事件---------
        window.setPosition(0, store.__const__.HEIGHT/2)
        
        ui.run(() => {
            dh.setBackgroundRoundRounded(window.box)
        })
        mutations.init_orerlay()
    
        //监听事件----------
        //主图标滑动监听
        window.st.setOnTouchListener(function(view, event){
            switch (event.getAction()) {
                case event.ACTION_DOWN:
                    x = event.getRawX()
                    y = event.getRawY()
                    windowX = window.getX()
                    windowY = window.getY()
                    downTime = new Date().getTime()
                    return true;
                case event.ACTION_MOVE:
                    //移动手指时调整位置
                    window.setPosition(windowX + (event.getRawX() - x), windowY + (event.getRawY() - y))
    
                    //如果按下的时间超过1.5秒 判断为长按
                    if (new Date().getTime() - downTime > 2000) {
                        // exit();
                    }
                    return true;
                case event.ACTION_UP:
                    //手指弹起的时候如果偏移很小判断为点击
                    if (Math.abs(event.getRawY() - y) < 5 && Math.abs(event.getRawX() - x) < 5){
                        OnstClick()
                    } else {
                        windowX = window.getX()
                        windowY = window.getY()
                        if (windowX != 0) {
                            console.log("归位")
                            window.setPosition(0, windowY)
                            //播放动画
                            dh.abs_animator(window.st)
                        }
                    }
                    return true;
            }
            return true
        })
    
        //图标主事件
        function OnstClick() {
            if (!store.__status__.window_status) {
                //打开
                dh.open_animator(window.box)
                window.st.setSource(mutations.getImageBase("robot-3-7"))
            } else {
                //隐藏
                dh.close_animator(window.box)
                window.st.setSource(mutations.getImageBase("Robot-1"))
            }
            store.__status__.window_status = !store.__status__.window_status
        }
    
        //启动和暂停
        window.run_stop.click(() => {
            //播放动画
            dh.click_animator(window.run_stop_icon)

            if (store.__script_path__ == "") {
                toastLog("请设置.run(脚本路径)")
                return null
            }
            if (!store.__status__.script_status) {
                //运行
                if (mutations.isEngineAlive()) mutations.stop_script();
                window.run_stop_icon.setSource(mutations.getImageBase("zanting"))
                //设置文字
                window.run_stop_text.setText("暂停")
                
                mutations.run_script(store.__script_path__)
                // console.log(mutations.isEngineAlive())
            
                OnstClick()
                
            } else {
                //停止
                if (mutations.isEngineAlive()) mutations.stop_script();
                window.run_stop_icon.setSource(mutations.getImageBase("bofang"))
                window.run_stop_text.setText("启动")

            }
            store.__status__.script_status = !store.__status__.script_status
        })
    
        // 日志
        window.log_icon.click(() => {
            //播放动画
            dh.click_animator(window.log_icon)
            
            if (!store.__status__.log_status) {
                window.log_icon.setSource(mutations.getImageBase("xianshi"))
                console.show()
            } else {
                window.log_icon.setSource(mutations.getImageBase("yincang"))
                console.hide()
    
            }
            store.__status__.log_status = !store.__status__.log_status
        })
    
        // 设置
        window.from.click(() => {
            console.log("设置")
            //升起动画
            if (store.__obj__.config_window.temp_main == null) {
                console.log("悬浮窗为空")
                return
            }
            ui.run(() => {
                dh.click_animator(window.from_icon)
                dh.open_pupwindow(store.__obj__.config_window.temp_main, function() {
                
                    t.store.__obj__.config_window.setSize(-1, -1)
                    t.mutations.open_orerlay()
                    
                })
            })

            store.__status__.setting_status = !store.__status__.setting_status
            
        })
    
        // 我的
        window.user.click(() => {
            //播放动画
            console.log("我的")
            ui.run(() => {
                dh.click_animator(window.user_icon)
        
                dh.open_pupwindow(store.__obj__.user_window.temp_main, function () {
                    t.store.__obj__.user_window.setSize(-1, -1)
                    t.mutations.open_orerlay()
                })
            })

            store.__status__.user_status = !store.__status__.user_status
        })
    
        // 退出
        window.quit.click(() => {
            //播放动画
            dh.click_animator(window.quit_icon)
            toastLog("退出")
            engines.stopAll()
            
        })
    }
}